<template>
  <el-container>
    <el-card class="box-card-wrap">
      <el-row :gutter="20">
        <el-tabs v-model="activeName">
          <el-tab-pane name="first" label="基本配置" :disabled="firstDisabled">
            <avue-form ref="form" :option="firstOption" v-model="form"></avue-form>
            <el-button @click="next" type="primary">
              下一步
            </el-button>
          </el-tab-pane>
          <el-tab-pane name="second" :disabled="secondDisabled" label="规则设计">
            <flow-design ref="flowDesign" v-if="!secondDisabled" :data="form"></flow-design>
            <el-button @click="prev" type="primary">
              上一步
            </el-button>
            <el-button @click="save" type="primary">
              保存
            </el-button>
          </el-tab-pane>
        </el-tabs>
      </el-row>
    </el-card>
  </el-container>
</template>
<script>
import {
  add,
} from "@/api/system/ruleManage";
import flowDesign from "./components/flowDesign";
export default {
  components: {
    flowDesign,
  },
  data() {
    return {
      form: {
        ruleName: '',
        ruleType: '',
        scope: '',
        ruleDescribe: '',
        abnormalCause: ''
      },
      firstOption: {
        submitBtn: false,
        emptyBtn: false,
        column: [
          {
            label: "规则名称",
            prop: "ruleName",
            rules: [{
              required: true,
              message: "请输入规则名称",
              trigger: "blur"
            }],
          },
          {
            label: "规则类型",
            prop: "ruleType",
            type: "select",
            dicUrl: '/api/blade-platform/rule/ruleType',
            rules: [{
              required: true,
              message: "请选择规则类型",
              trigger: "change"
            }]
          },
          {
            label: "成立阈值",
            prop: "scope",
            rules: [{
              required: true,
              message: "请输入成立阈值",
              trigger: "blur"
            }],
          },
          {
            label: "异常原因",
            prop: "abnormalCause",
          },
          {
            label: "说明",
            prop: "ruleDescribe",
          },
        ]
      },
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      activeName: 'first',
      firstDisabled: false,
      secondDisabled: true,
    }

  },
  mounted() {
    this.form.id = this.$route.query.id
    console.log('this.$route.query', this.$route.query);
  },
  methods: {
    save() {
      const flowData = this.$refs.flowDesign.getData()
      const item = {
        ruleName: this.form.ruleName,
        ruleType: this.form.ruleType,
        scope: this.form.scope,
        abnormalCause: this.form.abnormalCause,
        ruleDescribe: this.form.ruleDescribe,
        jsonTest: JSON.stringify({
          nodes: flowData.nodes,
          lines: flowData.lines,
        })
      }
      console.log('新增规则', JSON.stringify(item))
      add(item).then(() => {
        this.$message({
          type: "success",
          message: "操作成功!"
        });

        this.$router.push({
          path: '/system/ruleManage/index'
        })
      }, error => {
        window.console.log(error);
      });
    },
    prev() {
      this.activeName = 'first'
      this.secondDisabled = true
      this.firstDisabled = false
    },
    next() {
      this.$refs.form.validate((valid, done, msg) => {
        if (valid) {
          this.activeName = 'second'
          this.secondDisabled = false
          this.firstDisabled = true
          done()
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    }
  }
}
</script>
<style scoped>
.img-wrap {
  height: 280px;
  width: 100%;
}
</style>
<style>
.box-card-wrap {
  width: 100%;
  height: 100%;
}

.el-row {
  margin-bottom: 20px;

  &:last-child {
    margin-bottom: 0;
  }
}

.el-col {
  border-radius: 4px;
}

.bg-purple-dark {
  background: #99a9bf;
}

.bg-purple {
  background: #d3dce6;
}

.bg-purple-light {
  background: #e5e9f2;
}

.grid-content {
  border-radius: 4px;
  min-height: 36px;
}

.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}
</style>
